package c.e.a.b.b.h;

import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Looper;
import android.os.Message;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;
import c.e.a.b.f.b.i3;
import c.e.a.b.f.b.k3;
import c.e.a.b.f.b.r3;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Feature;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.common.internal.GetServiceRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.concurrent.GuardedBy;

public abstract class b<T extends IInterface> {
    public static final Feature[] u = new Feature[0];
    public o a;
    public final Context b;
    public final c c;
    public final c.e.a.b.b.b d;
    public final Handler e;
    public final Object f;
    public final Object g;
    @GuardedBy("mServiceBrokerLock")
    public g h;
    public c i;
    @GuardedBy("mLock")
    public T j;
    public final ArrayList<g<?>> k;
    @GuardedBy("mLock")
    public i l;
    @GuardedBy("mLock")
    public int m;
    public final a n;
    public final b o;
    public final int p;
    public final String q;
    public ConnectionResult r;
    public boolean s;
    public AtomicInteger t;

    public interface a {
        void b(Bundle bundle);

        void onConnectionSuspended(int i);
    }

    public interface b {
        void a(ConnectionResult connectionResult);
    }

    public interface c {
        void a(ConnectionResult connectionResult);
    }

    public abstract class g<TListener> {
        public TListener a;
        public boolean b = false;

        public g(TListener tListener) {
            this.a = tListener;
        }

        public final void a() {
            synchronized (this) {
                this.a = null;
            }
            synchronized (b.this.k) {
                b.this.k.remove(this);
            }
        }

        public abstract void b(TListener tListener);

        public abstract void c();
    }

    public final class i implements ServiceConnection {
        public final int a;

        public i(int i) {
            this.a = i;
        }

        public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            if (iBinder == null) {
                b.f(b.this);
                return;
            }
            synchronized (b.this.g) {
                g fVar;
                b bVar = b.this;
                IInterface queryLocalInterface = iBinder.queryLocalInterface("com.google.android.gms.common.internal.IGmsServiceBroker");
                if (queryLocalInterface == null || !(queryLocalInterface instanceof g)) {
                    fVar = new f(iBinder);
                } else {
                    fVar = (g) queryLocalInterface;
                }
                bVar.h = fVar;
            }
            b bVar2 = b.this;
            int i = this.a;
            Handler handler = bVar2.e;
            handler.sendMessage(handler.obtainMessage(7, i, -1, new k(0)));
        }

        public final void onServiceDisconnected(ComponentName componentName) {
            b bVar;
            synchronized (b.this.g) {
                bVar = b.this;
                bVar.h = null;
            }
            Handler handler = bVar.e;
            handler.sendMessage(handler.obtainMessage(6, this.a, 1));
        }
    }

    public class d implements c {
        public void a(ConnectionResult connectionResult) {
            if ((connectionResult.b == 0 ? 1 : null) != null) {
                b bVar = b.this;
                Objects.requireNonNull(bVar);
                Set set = Collections.EMPTY_SET;
                Objects.requireNonNull(bVar);
                Bundle bundle = new Bundle();
                GetServiceRequest getServiceRequest = new GetServiceRequest(bVar.p);
                getServiceRequest.d = bVar.b.getPackageName();
                getServiceRequest.g = bundle;
                if (set != null) {
                    getServiceRequest.f = (Scope[]) set.toArray(new Scope[set.size()]);
                }
                Feature[] featureArr = b.u;
                getServiceRequest.i = featureArr;
                getServiceRequest.j = featureArr;
                try {
                    synchronized (bVar.g) {
                        g gVar = bVar.h;
                        if (gVar != null) {
                            gVar.n0(new h(bVar, bVar.t.get()), getServiceRequest);
                        } else {
                            Log.w("GmsClient", "mServiceBroker is null, client disconnected");
                        }
                    }
                } catch (DeadObjectException e) {
                    Log.w("GmsClient", "IGmsServiceBroker.getService failed", e);
                    Handler handler = bVar.e;
                    handler.sendMessage(handler.obtainMessage(6, bVar.t.get(), 1));
                } catch (SecurityException e2) {
                    throw e2;
                } catch (RemoteException | RuntimeException e3) {
                    Log.w("GmsClient", "IGmsServiceBroker.getService failed", e3);
                    int i = bVar.t.get();
                    Handler handler2 = bVar.e;
                    handler2.sendMessage(handler2.obtainMessage(1, i, -1, new j(8, null, null)));
                }
                return;
            }
            b bVar2 = b.this.o;
            if (bVar2 != null) {
                bVar2.a(connectionResult);
            }
        }
    }

    public abstract class e extends g<Boolean> {
        public final int d;
        public final Bundle e;

        public e(int i, Bundle bundle) {
            super(Boolean.TRUE);
            this.d = i;
            this.e = bundle;
        }

        public final /* synthetic */ void b(Object obj) {
            Boolean bool = (Boolean) obj;
            int i = this.d;
            PendingIntent pendingIntent = null;
            if (i != 0) {
                if (i != 10) {
                    b.this.e(1, null);
                    Bundle bundle = this.e;
                    if (bundle != null) {
                        pendingIntent = (PendingIntent) bundle.getParcelable("pendingIntent");
                    }
                    d(new ConnectionResult(this.d, pendingIntent));
                    return;
                }
                b.this.e(1, null);
                r1 = new Object[3];
                Objects.requireNonNull(b.this);
                r1[1] = "com.google.android.gms.measurement.START";
                Objects.requireNonNull(b.this);
                r1[2] = "com.google.android.gms.measurement.internal.IMeasurementService";
                throw new IllegalStateException(String.format("A fatal developer error has occurred. Class name: %s. Start service action: %s. Service Descriptor: %s. ", r1));
            } else if (!e()) {
                b.this.e(1, null);
                d(new ConnectionResult(8, null));
            }
        }

        public final void c() {
        }

        public abstract void d(ConnectionResult connectionResult);

        public abstract boolean e();
    }

    public final class f extends c.e.a.b.e.b.d {
        public f(Looper looper) {
            super(looper);
        }

        public static boolean a(Message message) {
            int i = message.what;
            return i == 2 || i == 1 || i == 7;
        }

        /* JADX WARNING: Missing block: B:12:0x002f, code skipped:
            if (r0 == 5) goto L_0x0031;
     */
        public final void handleMessage(android.os.Message r8) {
            /*
            r7 = this;
            r0 = c.e.a.b.b.h.b.this;
            r0 = r0.t;
            r0 = r0.get();
            r1 = r8.arg1;
            if (r0 == r1) goto L_0x001d;
        L_0x000c:
            r0 = a(r8);
            if (r0 == 0) goto L_0x001c;
        L_0x0012:
            r8 = r8.obj;
            r8 = (c.e.a.b.b.h.b.g) r8;
            r8.c();
            r8.a();
        L_0x001c:
            return;
        L_0x001d:
            r0 = r8.what;
            r1 = 4;
            r2 = 1;
            r3 = 5;
            if (r0 == r2) goto L_0x0031;
        L_0x0024:
            r4 = 7;
            if (r0 == r4) goto L_0x0031;
        L_0x0027:
            if (r0 != r1) goto L_0x002f;
        L_0x0029:
            r0 = c.e.a.b.b.h.b.this;
            java.util.Objects.requireNonNull(r0);
            goto L_0x0031;
        L_0x002f:
            if (r0 != r3) goto L_0x0044;
        L_0x0031:
            r0 = c.e.a.b.b.h.b.this;
            r0 = r0.d();
            if (r0 != 0) goto L_0x0044;
        L_0x0039:
            r8 = r8.obj;
            r8 = (c.e.a.b.b.h.b.g) r8;
            r8.c();
            r8.a();
            return;
        L_0x0044:
            r0 = r8.what;
            r4 = 8;
            r5 = 3;
            r6 = 0;
            if (r0 != r1) goto L_0x0081;
        L_0x004c:
            r0 = c.e.a.b.b.h.b.this;
            r1 = new com.google.android.gms.common.ConnectionResult;
            r8 = r8.arg2;
            r1.m8169init(r8);
            r0.r = r1;
            r8 = c.e.a.b.b.h.b.h(r0);
            if (r8 == 0) goto L_0x0067;
        L_0x005d:
            r8 = c.e.a.b.b.h.b.this;
            r0 = r8.s;
            if (r0 != 0) goto L_0x0067;
        L_0x0063:
            r8.e(r5, r6);
            return;
        L_0x0067:
            r8 = c.e.a.b.b.h.b.this;
            r0 = r8.r;
            if (r0 == 0) goto L_0x006e;
        L_0x006d:
            goto L_0x0073;
        L_0x006e:
            r0 = new com.google.android.gms.common.ConnectionResult;
            r0.m8169init(r4);
        L_0x0073:
            r8 = r8.i;
            r8.a(r0);
            r8 = c.e.a.b.b.h.b.this;
            java.util.Objects.requireNonNull(r8);
            java.lang.System.currentTimeMillis();
            return;
        L_0x0081:
            if (r0 != r3) goto L_0x009d;
        L_0x0083:
            r8 = c.e.a.b.b.h.b.this;
            r0 = r8.r;
            if (r0 == 0) goto L_0x008a;
        L_0x0089:
            goto L_0x008f;
        L_0x008a:
            r0 = new com.google.android.gms.common.ConnectionResult;
            r0.m8169init(r4);
        L_0x008f:
            r8 = r8.i;
            r8.a(r0);
            r8 = c.e.a.b.b.h.b.this;
            java.util.Objects.requireNonNull(r8);
            java.lang.System.currentTimeMillis();
            return;
        L_0x009d:
            if (r0 != r5) goto L_0x00bf;
        L_0x009f:
            r0 = r8.obj;
            r1 = r0 instanceof android.app.PendingIntent;
            if (r1 == 0) goto L_0x00a8;
        L_0x00a5:
            r6 = r0;
            r6 = (android.app.PendingIntent) r6;
        L_0x00a8:
            r0 = new com.google.android.gms.common.ConnectionResult;
            r8 = r8.arg2;
            r0.m8171init(r8, r6);
            r8 = c.e.a.b.b.h.b.this;
            r8 = r8.i;
            r8.a(r0);
            r8 = c.e.a.b.b.h.b.this;
            java.util.Objects.requireNonNull(r8);
            java.lang.System.currentTimeMillis();
            return;
        L_0x00bf:
            r1 = 6;
            if (r0 != r1) goto L_0x00e0;
        L_0x00c2:
            r0 = c.e.a.b.b.h.b.this;
            r0.e(r3, r6);
            r0 = c.e.a.b.b.h.b.this;
            r0 = r0.n;
            if (r0 == 0) goto L_0x00d2;
        L_0x00cd:
            r8 = r8.arg2;
            r0.onConnectionSuspended(r8);
        L_0x00d2:
            r8 = c.e.a.b.b.h.b.this;
            java.util.Objects.requireNonNull(r8);
            java.lang.System.currentTimeMillis();
            r8 = c.e.a.b.b.h.b.this;
            c.e.a.b.b.h.b.g(r8, r3, r2, r6);
            return;
        L_0x00e0:
            r1 = 2;
            if (r0 != r1) goto L_0x00f6;
        L_0x00e3:
            r0 = c.e.a.b.b.h.b.this;
            r0 = r0.c();
            if (r0 != 0) goto L_0x00f6;
        L_0x00eb:
            r8 = r8.obj;
            r8 = (c.e.a.b.b.h.b.g) r8;
            r8.c();
            r8.a();
            return;
        L_0x00f6:
            r0 = a(r8);
            if (r0 == 0) goto L_0x014a;
        L_0x00fc:
            r8 = r8.obj;
            r0 = r8;
            r0 = (c.e.a.b.b.h.b.g) r0;
            monitor-enter(r0);
            r8 = r0.a;	 Catch:{ all -> 0x0147 }
            r1 = r0.b;	 Catch:{ all -> 0x0147 }
            if (r1 == 0) goto L_0x012d;
        L_0x0108:
            r1 = "GmsClient";
            r3 = java.lang.String.valueOf(r0);	 Catch:{ all -> 0x0147 }
            r4 = r3.length();	 Catch:{ all -> 0x0147 }
            r4 = r4 + 47;
            r5 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0147 }
            r5.<init>(r4);	 Catch:{ all -> 0x0147 }
            r4 = "Callback proxy ";
            r5.append(r4);	 Catch:{ all -> 0x0147 }
            r5.append(r3);	 Catch:{ all -> 0x0147 }
            r3 = " being reused. This is not safe.";
            r5.append(r3);	 Catch:{ all -> 0x0147 }
            r3 = r5.toString();	 Catch:{ all -> 0x0147 }
            android.util.Log.w(r1, r3);	 Catch:{ all -> 0x0147 }
        L_0x012d:
            monitor-exit(r0);	 Catch:{ all -> 0x0147 }
            if (r8 == 0) goto L_0x0139;
        L_0x0130:
            r0.b(r8);	 Catch:{ RuntimeException -> 0x0134 }
            goto L_0x013c;
        L_0x0134:
            r8 = move-exception;
            r0.c();
            throw r8;
        L_0x0139:
            r0.c();
        L_0x013c:
            monitor-enter(r0);
            r0.b = r2;	 Catch:{ all -> 0x0144 }
            monitor-exit(r0);	 Catch:{ all -> 0x0144 }
            r0.a();
            return;
        L_0x0144:
            r8 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x0144 }
            throw r8;
        L_0x0147:
            r8 = move-exception;
            monitor-exit(r0);	 Catch:{ all -> 0x0147 }
            throw r8;
        L_0x014a:
            r0 = "GmsClient";
            r8 = r8.what;
            r1 = 45;
            r2 = new java.lang.StringBuilder;
            r2.<init>(r1);
            r1 = "Don't know how to handle message: ";
            r2.append(r1);
            r2.append(r8);
            r8 = r2.toString();
            r1 = new java.lang.Exception;
            r1.<init>();
            android.util.Log.wtf(r0, r8, r1);
            return;
            */
            throw new UnsupportedOperationException("Method not decompiled: c.e.a.b.b.h.b$f.handleMessage(android.os.Message):void");
        }
    }

    public static final class h extends c.e.a.b.b.h.e.a {
        public b a;
        public final int b;

        public h(b bVar, int i) {
            this.a = bVar;
            this.b = i;
        }

        public final void J0(int i, IBinder iBinder, Bundle bundle) {
            b.i.b.f.S(this.a, "onPostInitComplete can be called only once per call to getRemoteService");
            b bVar = this.a;
            int i2 = this.b;
            Handler handler = bVar.e;
            handler.sendMessage(handler.obtainMessage(1, i2, -1, new j(i, iBinder, bundle)));
            this.a = null;
        }
    }

    public final class j extends e {
        public final IBinder g;

        public j(int i, IBinder iBinder, Bundle bundle) {
            super(i, bundle);
            this.g = iBinder;
        }

        public final void d(ConnectionResult connectionResult) {
            b bVar = b.this.o;
            if (bVar != null) {
                bVar.a(connectionResult);
            }
            Objects.requireNonNull(b.this);
            System.currentTimeMillis();
        }

        public final boolean e() {
            String str = "GmsClient";
            try {
                String interfaceDescriptor = this.g.getInterfaceDescriptor();
                Objects.requireNonNull(b.this);
                String str2 = "com.google.android.gms.measurement.internal.IMeasurementService";
                if (str2.equals(interfaceDescriptor)) {
                    IInterface iInterface;
                    b bVar = b.this;
                    IBinder iBinder = this.g;
                    Objects.requireNonNull((r3) bVar);
                    if (iBinder == null) {
                        iInterface = null;
                    } else {
                        iInterface = iBinder.queryLocalInterface(str2);
                        if (iInterface instanceof i3) {
                            iInterface = (i3) iInterface;
                        } else {
                            iInterface = new k3(iBinder);
                        }
                    }
                    if (iInterface == null || (!b.g(b.this, 2, 4, iInterface) && !b.g(b.this, 3, 4, iInterface))) {
                        return false;
                    }
                    b bVar2 = b.this;
                    bVar2.r = null;
                    a aVar = bVar2.n;
                    if (aVar != null) {
                        aVar.b(null);
                    }
                    return true;
                }
                Objects.requireNonNull(b.this);
                StringBuilder stringBuilder = new StringBuilder(String.valueOf(interfaceDescriptor).length() + 97);
                stringBuilder.append("service descriptor mismatch: ");
                stringBuilder.append(str2);
                stringBuilder.append(" vs. ");
                stringBuilder.append(interfaceDescriptor);
                Log.e(str, stringBuilder.toString());
                return false;
            } catch (RemoteException unused) {
                Log.w(str, "service probably died");
                return false;
            }
        }
    }

    public final class k extends e {
        public k(int i) {
            super(i, null);
        }

        public final void d(ConnectionResult connectionResult) {
            Objects.requireNonNull(b.this);
            b.this.i.a(connectionResult);
            Objects.requireNonNull(b.this);
            System.currentTimeMillis();
        }

        public final boolean e() {
            b.this.i.a(ConnectionResult.e);
            return true;
        }
    }

    public b(Context context, Looper looper, int i, a aVar, b bVar, String str) {
        synchronized (c.a) {
            if (c.b == null) {
                c.b = new m(context.getApplicationContext());
            }
        }
        c cVar = c.b;
        c.e.a.b.b.b bVar2 = c.e.a.b.b.b.b;
        Objects.requireNonNull(aVar, "null reference");
        Objects.requireNonNull(bVar, "null reference");
        this.f = new Object();
        this.g = new Object();
        this.k = new ArrayList();
        this.m = 1;
        this.r = null;
        this.s = false;
        this.t = new AtomicInteger(0);
        b.i.b.f.S(context, "Context must not be null");
        this.b = context;
        b.i.b.f.S(looper, "Looper must not be null");
        b.i.b.f.S(cVar, "Supervisor must not be null");
        this.c = cVar;
        b.i.b.f.S(bVar2, "API availability must not be null");
        this.d = bVar2;
        this.e = new f(looper);
        this.p = i;
        this.n = aVar;
        this.o = bVar;
        this.q = null;
    }

    public static void f(b bVar) {
        Object obj;
        int i;
        synchronized (bVar.f) {
            obj = bVar.m == 3 ? 1 : null;
        }
        if (obj != null) {
            i = 5;
            bVar.s = true;
        } else {
            i = 4;
        }
        Handler handler = bVar.e;
        handler.sendMessage(handler.obtainMessage(i, bVar.t.get(), 16));
    }

    public static boolean g(b bVar, int i, int i2, IInterface iInterface) {
        boolean z;
        synchronized (bVar.f) {
            if (bVar.m != i) {
                z = false;
            } else {
                bVar.e(i2, iInterface);
                z = true;
            }
        }
        return z;
    }

    public static boolean h(b bVar) {
        if (bVar.s) {
            return false;
        }
        String str = "com.google.android.gms.measurement.internal.IMeasurementService";
        if (TextUtils.isEmpty(str) || TextUtils.isEmpty(null)) {
            return false;
        }
        try {
            Class.forName(str);
            return true;
        } catch (ClassNotFoundException unused) {
            return false;
        }
    }

    public void a() {
        int a = this.d.a(this.b, 12451000);
        String str = "Connection progress callbacks cannot be null.";
        if (a != 0) {
            e(1, null);
            d dVar = new d();
            b.i.b.f.S(dVar, str);
            this.i = dVar;
            Handler handler = this.e;
            handler.sendMessage(handler.obtainMessage(3, this.t.get(), a, null));
            return;
        }
        d dVar2 = new d();
        b.i.b.f.S(dVar2, str);
        this.i = dVar2;
        e(2, null);
    }

    public final T b() throws DeadObjectException {
        IInterface iInterface;
        synchronized (this.f) {
            if (this.m == 5) {
                throw new DeadObjectException();
            } else if (c()) {
                b.i.b.f.U(this.j != null, "Client is connected but service is null");
                iInterface = this.j;
            } else {
                throw new IllegalStateException("Not connected. Call connect() and wait for onConnected() to be called.");
            }
        }
        return iInterface;
    }

    public boolean c() {
        boolean z;
        synchronized (this.f) {
            z = this.m == 4;
        }
        return z;
    }

    public boolean d() {
        boolean z;
        synchronized (this.f) {
            int i = this.m;
            if (i != 2) {
                if (i != 3) {
                    z = false;
                }
            }
            z = true;
        }
        return z;
    }

    public final void e(int i, T t) {
        b.i.b.f.J((i == 4 ? 1 : null) == (t != null ? 1 : null));
        synchronized (this.f) {
            this.m = i;
            this.j = t;
            String str;
            c cVar;
            String str2;
            String str3;
            if (i != 1) {
                if (i == 2 || i == 3) {
                    String str4;
                    String str5;
                    if (!(this.l == null || this.a == null)) {
                        str4 = "GmsClient";
                        str = "com.google.android.gms.measurement.START";
                        str5 = "com.google.android.gms";
                        StringBuilder stringBuilder = new StringBuilder((str.length() + 70) + str5.length());
                        stringBuilder.append("Calling connect() while still connected, missing disconnect() for ");
                        stringBuilder.append(str);
                        stringBuilder.append(" on ");
                        stringBuilder.append(str5);
                        Log.e(str4, stringBuilder.toString());
                        cVar = this.c;
                        Objects.requireNonNull(this.a);
                        str = "com.google.android.gms.measurement.START";
                        str5 = "com.google.android.gms";
                        i iVar = this.l;
                        String i2 = i();
                        Objects.requireNonNull(cVar);
                        cVar.b(new c.e.a.b.b.h.c.a(str, str5, 129), iVar, i2);
                        this.t.incrementAndGet();
                    }
                    i iVar2 = new i(this.t.get());
                    this.l = iVar2;
                    str5 = "com.google.android.gms";
                    str2 = "com.google.android.gms.measurement.START";
                    this.a = new o(str5, str2, false);
                    if (!this.c.a(new c.e.a.b.b.h.c.a(str2, str5, 129), iVar2, i())) {
                        str4 = "GmsClient";
                        Objects.requireNonNull(this.a);
                        String str6 = "com.google.android.gms.measurement.START";
                        str3 = "com.google.android.gms";
                        StringBuilder stringBuilder2 = new StringBuilder((str6.length() + 34) + str3.length());
                        stringBuilder2.append("unable to connect to service: ");
                        stringBuilder2.append(str6);
                        stringBuilder2.append(" on ");
                        stringBuilder2.append(str3);
                        Log.e(str4, stringBuilder2.toString());
                        int i3 = this.t.get();
                        Handler handler = this.e;
                        handler.sendMessage(handler.obtainMessage(7, i3, -1, new k(16)));
                    }
                } else if (i == 4) {
                    System.currentTimeMillis();
                }
            } else if (this.l != null) {
                cVar = this.c;
                Objects.requireNonNull(this.a);
                str3 = "com.google.android.gms.measurement.START";
                str = "com.google.android.gms";
                i iVar3 = this.l;
                str2 = i();
                Objects.requireNonNull(cVar);
                cVar.b(new c.e.a.b.b.h.c.a(str3, str, 129), iVar3, str2);
                this.l = null;
            }
        }
    }

    public final String i() {
        String str = this.q;
        return str == null ? this.b.getClass().getName() : str;
    }
}
